<?php namespace Summer\Sms\Components;

use Auth;
use Lang;
use Mail;
use Flash;
use Cache;
use Summer\Sms\Classes\SmsApi;
use Summer\Sms\Models\Settings;
use Validator;
use ValidationException;
use ApplicationException;
use Input;
use Response;
use Cms\Classes\ComponentBase;
use System\Models\File as FileModel;
use Summer\Hjd\Classes\AspxSend;


class Sms extends ComponentBase
{
    public function componentDetails()
    {
        return [
            'name'        => '短信服务',
            'description' => '短信发送'
        ];
    }

    public function defineProperties()
    {
        return [

        ];
    }

    /**
     * Returns the logged in user, if available
     */
    public function user()
    {
        if (!Auth::check()) {
            return null;
        }
        return Auth::getUser();
    }

    /**
     * 发送验证码
     * @return array
     */
    public function onSendCode()
    {
        $return = [];
        $return['status'] = -1;
        $return['msg'] = '操作失败';
        $return['data'] = [];

        $data = post();
        $phone=$data['phone'];
        $code=mt_rand(1000, 9999);
        $minutes=60*5;
//        Cache::put('code_'.$tel, $code, $minutes);
        //这里调用发送验证码
        $sms_config=Settings::instance();
        if($sms_config->is_check!=1){
            $return['status']=1;
            $return['msg']='操作成功.短信验证关闭';
            return $return;
        }else{
            $sms_api=new SmsApi();
            $scene_slug='base_code';
            $sms_api->send($phone,$scene_slug,['code'=>$code]);
            $return['status']=1;
            $return['msg']='操作成功.';
            return $return;
        }

    }

    /**
     * 用户收款码
     */
    public function onUserSk(){
        if (!$user = $this->user()) {
            return;
        }
        $rules = [
            'shoukuan_type' => 'required',
            'shoukuan_account' => 'required',
            'cover_shoukuan_id' => 'required',
            'verify_code' => 'required',
        ];
        $messages = [
            'shoukuan_type.required'    => '收款方式不能为空.',
            'shoukuan_account.required'    => '收款账号不能为空.',
            'cover_shoukuan_id.required'    => '请上传收款码.',
            'verify_code.required'    => '短信验证码不能为空.',
        ];
        $validation = Validator::make(post(), $rules,$messages);
        if ($validation->fails()) {
            throw new ValidationException($validation);
        }
        $data = post();

        $code = Cache::get('code_'.$user['userphone']);
        $captcha=$data['verify_code'];
        if($code!=$captcha){
            throw new ValidationException(['verify_code'=>'验证码错误']);
        }

        $attachment_id=$user->id;
        $attachment_type='RainLab\User\Models\User';
        $fileid=$data['cover_shoukuan_id'];
        //关联图片
        $field='cover_shoukuan';
        //删除原图片
        FileModel::where('attachment_id',$attachment_id)->where('field',$field)->where('attachment_type',$attachment_type)->where('id','<>',$fileid)->delete();
        FileModel::where('id',$fileid)->update(['attachment_id'=>$attachment_id,'field'=>$field,'attachment_type'=>$attachment_type]);
        unset($data['cover_shoukuan_id']);
        $data['status_sk']=2;
        $data['sk_up_at']=time();
        $user->fill($data);
        $user->save();
        Flash::success('操作成功!');

    }

}
